package code.c02_list.linkList;

import code.c02_list.linkList.LinkListClass;
import code.c02_list.clinkList.LinkNode;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/9/18 17:37
 * @package code.c02_list.classwork.examples
 * @description 有一个长度大于2 的整数链表L ，求中间位置元素。
 *
 */

public class Exam_2_10 {
    public static int Middle1(LinkListClass<Integer> L ){
        int j =1,n = L.size();
        LinkNode<Integer> p = L.head.next;
        while (j <= (n-1)/2){
            j ++;
            p=p.next;
        }
        return p.data;
    }

    public static int Middle2(LinkListClass<Integer> L){
        LinkNode<Integer> slow = L.head.next;
        LinkNode<Integer> fast = L.head.next;

        while (fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }

        return slow.data;
    }
}
